import 'package:flutter/material.dart';

import '../../repository/sjx_api.dart';

class FeedbackPage extends StatefulWidget {
  const FeedbackPage({super.key});

  @override
  State<FeedbackPage> createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  final TextEditingController _feedbackController = TextEditingController();
  final int _maxLength = 200;
  int _currentLength = 0;

  @override
  void initState() {
    super.initState();
    _feedbackController.addListener(_updateLength);
  }

  @override
  void dispose() {
    _feedbackController.dispose();
    super.dispose();
  }

  void _updateLength() {
    setState(() {
      _currentLength = _feedbackController.text.length;
    });
  }

  Future<void> _submitFeedback() async {
    final String feedback = _feedbackController.text.trim();

    if (feedback.isEmpty) {
      _showMessage('请输入反馈内容');
      return;
    }

    if (feedback.length < 5) {
      _showMessage('反馈内容至少5个字');
      return;
    }

    await SJXApi.instance().feedback(feedback);
    _showSuccessDialog();
  }

  void _showMessage(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
        duration: const Duration(seconds: 2),
      ),
    );
  }

  void _showSuccessDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('提交成功'),
          content: const Text('感谢您的反馈，我们会认真考虑您的建议！'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
                Navigator.of(context).pop(); // 返回上一页
              },
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
  }

  void _clearInput() {
    _feedbackController.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('意见反馈'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
        ),
        actions: [
          if (_feedbackController.text.isNotEmpty)
            IconButton(
              icon: const Icon(Icons.clear),
              onPressed: _clearInput,
              tooltip: '清空',
            ),
        ],
      ),
      backgroundColor: const Color.fromRGBO(244, 247, 250, 1),
      body: Column(
        children: [
          // 输入区域
          Container(
            margin: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.05),
                  blurRadius: 4,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题
                const Padding(
                  padding: EdgeInsets.fromLTRB(16, 16, 16, 8),
                  child: Text(
                    '请输入您的意见或建议',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: Color(0xFF262626),
                    ),
                  ),
                ),
                // 文本输入框
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: TextField(
                    controller: _feedbackController,
                    maxLines: 8,
                    maxLength: _maxLength,
                    decoration: const InputDecoration(
                      border: InputBorder.none,
                      hintText: '请输入您的宝贵意见...（至少5个字）',
                      hintStyle: TextStyle(
                        color: Color(0xFFBFBFBF),
                        fontSize: 14,
                      ),
                      contentPadding: EdgeInsets.symmetric(vertical: 12),
                    ),
                    style: const TextStyle(
                      fontSize: 14,
                      color: Color(0xFF262626),
                    ),
                  ),
                ),
                // 字数统计
                Padding(
                  padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                    ],
                  ),
                ),
              ],
            ),
          ),
          // 提示信息
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 16),
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: const Color(0xFFE6F7FF),
              borderRadius: BorderRadius.circular(6),
              border: Border.all(
                color: const Color(0xFF91D5FF),
                width: 1,
              ),
            ),
            child: const Row(
              children: [
                Icon(
                  Icons.info_outline,
                  size: 16,
                  color: Color(0xFF1890FF),
                ),
                SizedBox(width: 8),
                Expanded(
                  child: Text(
                    '您的反馈将帮助我们改进产品和服务',
                    style: TextStyle(
                      fontSize: 12,
                      color: Color(0xFF1890FF),
                    ),
                  ),
                ),
              ],
            ),
          ),

        ],
      ),
      bottomNavigationBar: SafeArea(
        child: Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
              top: BorderSide(
                color: Colors.grey.shade200,
                width: 1,
              ),
            ),
          ),
          child: SizedBox(
            width: double.infinity,
            height: 44,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(22),
                ),
                elevation: 0,
              ),
              onPressed: _submitFeedback,
              child: const Text(
                '提交反馈',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}